// require("../themes/js/jquery.js");
// require("../themes/js/bootstrap.min.js");
// require("../themes/js/google-code-prettify/prettify.js");
// require("../themes/js/bootshop.js");
// require("../themes/js/jquery.lightbox-0.5.js");
var _header_service = require('../../../service/header-service')
var _product_service = require('../../../service/product-service')
var _cart_service = require('../../../service/cart-service')
const vue = require('../vue')
const _util = require('../../../util/util');
let _sidebar_content = new vue({
    el: '#sidebar',
    data() {
        return {
            categories: '',
            products_0: [],
            products_1: [],
            products_2: [],
            products_3: [],
            products_4: [],
            products_5: [],
            products_6: [],
            products_7: [],
            cartItemsNum: 0,

        }
    },
    methods: {
        getCategories: function () {
            return this.categories
        },
        getProducts: function (id) {
            return this.idmap(id);
        },
        idmap: function (id) {
            switch (id) {
                case 0:
                    return this.products_0;
                case 1:
                    return this.products_1;
                case 2:
                    return this.products_2;
                case 3:
                    return this.products_3;
                case 4:
                    return this.products_4;
                case 5:
                    return this.products_5;
                case 6:
                    return this.products_6;
                case 7:
                    return this.products_7;
                default:
                    break;
            }
        },
        getCartNums: function () {
            let self = this;
            if (_util.checkLogin()) {
                _cart_service.getCartNums(
                    function (res) {
                        self.cartItemsNum = res;
                    },
                    function (errMsg) {
                        console.log(errMsg)
                    }
                )
            }
            return this.cartItemsNum
        },
    },
})
var _sidebar = {
    _this: this,
    init: function () {
        this.bindEvents();
        this.loadSortInfo();
        for (let i = 0; i < 8; i++) {
            this.loadProducts(i + 1);
        }
        setTimeout(() => {
            $('.subMenu > a').on("click", function (e) {
                e.preventDefault();
                var subMenu = $(this).siblings('ul');
                var li = $(this).parents('li');
                var subMenus = $('#sidebar li.subMenu ul');
                var subMenus_parents = $('#sidebar li.subMenu');
                if (li.hasClass('open')) {
                    if (($(window).width() > 768) || ($(window).width() < 479)) {
                        subMenu.slideUp();
                    } else {
                        subMenu.fadeOut(250);
                    }
                    li.removeClass('open');
                } else {
                    if (($(window).width() > 768) || ($(window).width() < 479)) {
                        subMenus.slideUp();
                        subMenu.slideDown();
                    } else {
                        subMenus.fadeOut(250);
                        subMenu.fadeIn(250);
                    }
                    subMenus_parents.removeClass('open');
                    li.addClass('open');
                }
            });
        }, 2000);
    },
    bindEvents: function () {

    },
    loadSortInfo: function () {
        _header_service.getAllCategories(
            function (res) {
                _sidebar_content.categories = res;
            },
            function (errMsg) {
                console.log("failed!")
            },
        )
    },
    loadProducts: function (id) {
        _product_service.getProducts(
            id,
            function (res) {
                switch (id - 1) {
                    case 0:
                        _sidebar_content.products_0 = res; break;
                    case 1:
                        _sidebar_content.products_1 = res; break;
                    case 2:
                        _sidebar_content.products_2 = res; break;
                    case 3:
                        _sidebar_content.products_3 = res; break;
                    case 4:
                        _sidebar_content.products_4 = res; break;
                    case 5:
                        _sidebar_content.products_5 = res; break;
                    case 6:
                        _sidebar_content.products_6 = res; break;
                    case 7:
                        _sidebar_content.products_7 = res; break;
                    default:
                        break;
                }
            },
            function (errMsg) {
                console.log("failed!");
            }
        )
    }
}
module.exports = _sidebar.init();